﻿Imports Microsoft.Kinect

Class MainWindow
    Dim sensor As KinectSensor
    Dim PixelData() As Short

    Private Sub Window_Loaded(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
        sensor = Microsoft.Kinect.KinectSensor.KinectSensors(0)
        sensor.DepthStream.Enable()
        AddHandler sensor.DepthFrameReady, AddressOf Sensor_DepthFrameReady
        sensor.Start()
    End Sub

    Private Sub Window_Unoaded(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles MyBase.Unloaded
        sensor.Stop()
    End Sub

    Private Sub Sensor_DepthFrameReady(sender As Object, e As DepthImageFrameReadyEventArgs)

        Dim ReceivedData As Boolean = False

        Using DFrame As DepthImageFrame = e.OpenDepthImageFrame()
            If DFrame Is Nothing Then
                ' The image processing took too long. More than 2 frames behind.
            Else
                ReDim PixelData(DFrame.PixelDataLength - 1)
                DFrame.CopyPixelDataTo(PixelData)
                ReceivedData = True
            End If
        End Using

        If ReceivedData Then
            Dim source As BitmapSource = BitmapSource.Create(640, 480, 96, 96,
    PixelFormats.Gray16, Nothing, PixelData, 640 * 2)

            depthImage.Source = source
        End If
    End Sub
End Class
